<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>角色管理</span>
      </div>
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <div class="app-container">
          <div class="filter-container">
            <el-input
              v-model="where.roleName"
              class="filter-item"
              clearable
              placeholder="角色名称" style="width: 200px;"
              @clear="reload"/>

            <el-input
              v-model="where.roleCode"
              class="filter-item"
              clearable
              placeholder="角色标识" style="width: 200px;"
              @clear="reload"/>

            <el-input
              v-model="where.comments"
              class="filter-item"
              clearable
              placeholder="备注" style="width: 200px;"
              @clear="reload"/>

            <el-button

              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload">搜索
            </el-button>

            <el-button class="filter-item" icon="el-icon-refresh" style="margin-left: 2px;" @click="reset">重置
            </el-button>

          </div>
        </div>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :selection.sync="selection"
        :where="where">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            v-if="$hasPermission('role:save')"

            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openEdit({})">添加
          </el-button>
          <el-button
            v-if="$hasPermission('role:delete')"

            icon="el-icon-delete"
            size="small"
            type="danger"
            @click="removeBatch">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            v-if="$hasPermission('role:update')"
            :underline="false"
            icon="el-icon-edit-outline"
            type="primary"
            @click="openEdit(row)">修改
          </el-link>
          <el-link
            v-if="$hasPermission('role:auth')"
            :underline="false"
            icon="el-icon-finished"
            type="primary"
            @click="openAuth(row)">分配权限
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此角色吗？"
            @confirm="remove(row)">
            <el-link
              v-if="$hasPermission('role:delete')"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              type="danger">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
    <!-- 权限分配弹窗 -->
    <role-auth
      :data="current"
      :visible.sync="showAuth"/>
  </div>
</template>

<script>
import RoleEdit from './role-edit';
import RoleAuth from './role-auth';

export default {
  name: 'SystemRole',
  components: {RoleEdit, RoleAuth},
  data() {
    return {
      // 表格高度
      defaultHeight: 0,
      // 表格数据接口
      url: '/role/role',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center'
        },
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'roleName',
          label: '角色名称',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'roleCode',
          label: '角色标识',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'comments',
          label: '备注',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'createTime',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          resizable: false,
          slot: 'action'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showAuth: false
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = {where: this.where};
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示分配权限 */
    openAuth(row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/role/role/id/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的角色吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        let arrId = this.selection.map(d => d.id);
        this.$http.delete('/role/role/batch', {
          data: {id: arrId.join(',')}
        }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    }
  }
}
</script>

<style scoped>

</style>
